﻿using System;
using fastCSharp;

namespace diantou.web.tutorial
{
    /// <summary>
    /// 用户课程
    /// </summary>
    [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.userCourse")]
    struct userCourse
    {
        /// <summary>
        /// 课程
        /// </summary>
        public diantou.dataAccess.course Course;
        /// <summary>
        /// 用户信息
        /// </summary>
        public diantou.dataAccess.user User;
        /// <summary>
        /// 课程标识
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Id
        {
            get { return Course == null ? 0 : Course.Id; }
        }
        /// <summary>
        /// 用户标识
        /// </summary>
        internal int UserId
        {
            get { return User == null ? 0 : User.Id; }
        }
        /// <summary>
        /// 学习通过分页数量
        /// </summary>
        public int PassPageCount
        {
            get
            {
                if (User == null || Course == null) return 0;
                //return diantou.dataAccess.tcpCall.userCourse.get(User.Id, Course.Id);
                return diantou.dataAccess.tcpCall.userCourse.GetPassCount(User.Id, Course.Id);
            }
        }
        /// <summary>
        /// 是否编辑权限
        /// </summary>
        public bool IsPermission
        {
            get
            {
                if (User == null || Course == null) return false;
                if (User.Permission == dataModel.user.permission.Admin) return true;
                return (User.Permission & dataModel.user.permission.TutorialEditor) != 0 && diantou.dataAccess.tcpCall.tutorial.isEditor(User.Id, Course.TutorialId);
            }
        }
        /// <summary>
        /// 课程是否锁定状态
        /// </summary>
        public bool IsLock
        {
            get
            {
                if (User == null || Course == null) return true;
                return diantou.dataAccess.tcpCall.userCourse.GetLock(User.Id, Course.Id);
            }
        }
        /// <summary>
        /// 课程分页集合
        /// </summary>
        public diantou.dataAccess.coursePage[] Pages
        {
            get
            {
                if (Course == null) return nullValue<diantou.dataAccess.coursePage>.Array;
                diantou.dataAccess.coursePage[] pages = diantou.dataAccess.tcpCall.coursePage.getCourse(Course.Id);
                int count;
                if (User == null) count = 0;
                else count = diantou.dataAccess.tcpCall.userCourse.GetPassCount(User.Id, Course.Id);
                pages[Math.Min(count, pages.Length - 1)].IsCurrent = true;
                foreach (diantou.dataAccess.coursePage page in pages) page.IsLock = count-- < 0;
                return pages;
            }
        }
    }
}